package com.sqx.modules.ruleClassify.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.ruleClassify.dao.RuleClassifyDao;
import com.sqx.modules.ruleClassify.entity.RuleClassify;
import com.sqx.modules.ruleClassify.service.RuleClassifyService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 岗位分类
 */
@Service
public class RuleClassifyServiceImpl extends ServiceImpl<RuleClassifyDao, RuleClassify> implements RuleClassifyService {


    @Override
    public Result addClassify(RuleClassify ruleClassify) {
        if (ruleClassify.getParentId() != 0) {
            RuleClassify rule = baseMapper.selectById(ruleClassify.getParentId());
            if (rule == null) {
                return Result.error("一级菜单不存在");
            }
        }
        ruleClassify.setCreateTime(LocalDateTime.now());
        return Result.upStatus(baseMapper.insert(ruleClassify));
    }

    @Override
    public List<RuleClassify> getClassifyList(RuleClassify classify) {
        List<RuleClassify> records = baseMapper.selectList(new QueryWrapper<>(classify).eq("status", 1));
        return getClassifyTree(records);
    }


    @Override
    public IPage<RuleClassify> adminGetClassifyList(Integer page, Integer limit, RuleClassify classify) {
        QueryWrapper<RuleClassify> wrapper = new QueryWrapper<>(classify);
        Page<RuleClassify> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        if (classify.getParentId() == null) {
            wrapper.eq("parent_id", 0);
        }
        wrapper.orderByAsc("sort");
        return baseMapper.selectPage(pages, wrapper);
    }

    @Override
    public IPage<RuleClassify> adminGetParentId(Integer page, Integer limit, RuleClassify classify) {
        Page<RuleClassify> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<RuleClassify> selectPage = baseMapper.selectPage(pages, new QueryWrapper<>(classify).orderByAsc("sort"));
        for (RuleClassify record : selectPage.getRecords()) {
            record.setChildrens(baseMapper.selectList(new QueryWrapper<RuleClassify>().eq("parent_id", record.getRuleClassifyId()).orderByAsc("sort")));
        }
        return selectPage;
    }

    @Override
    public Result updateClassify(RuleClassify ruleClassify) {
        if (ruleClassify.getParentId()!=null && ruleClassify.getParentId() != 0) {
            RuleClassify classify = baseMapper.selectById(ruleClassify.getParentId());
            if (classify == null) {
                return Result.error("上级不存在");
            }
        }
        return Result.upStatus(baseMapper.updateById(ruleClassify));
    }

    @Override
    public Result deleteClassify(Long ruleClassifyId) {
        Integer count = baseMapper.selectCount(new QueryWrapper<RuleClassify>().eq("parent_id", ruleClassifyId));
        if (count > 0) {
            return Result.error("请先删除下级机械类型");
        } else {
            return Result.upStatus(baseMapper.deleteById(ruleClassifyId));
        }


    }

    @Override
    public List<RuleClassify> userGetClassify(RuleClassify classify) {

        return baseMapper.selectList(new QueryWrapper<>(classify).eq("status", 1));

    }

    private List<RuleClassify> getClassifyTree(List<RuleClassify> records) {
        return records.stream()
                .filter(e -> e.getParentId() == 0)
                .peek((menu) -> menu.setChildrens(getChildrenList(menu, records)))
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());
    }

    //递归查找所有菜单的子菜单
    private List<RuleClassify> getChildrenList(RuleClassify root, List<RuleClassify> all) {
        return all.stream().filter(categoryEntity -> categoryEntity.getParentId().equals(root.getRuleClassifyId())).peek(categoryEntity -> {
            //1、找到子菜单(递归)
            categoryEntity.setChildrens(getChildrenList(categoryEntity, all));
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
    }
}
